
#include "x_logfile.h"
#include "x_logger.h"
#include <memory>

void CLogFile::writeLog(const char * logmsg, int len)
{
    if (nullptr == m_logFile) return;
    fwrite_unlocked(logmsg, len, 1, m_logFile);
    m_writtenBytes += len;
    //日志文件备份
    if (m_writtenBytes >= DEFAULT_ROLL_SIZE)
    {
        m_isRoll = true;
        rollLogFile();
    }
}

void CLogFile::flushLog()
{
    if (nullptr == m_logFile) return;
    fflush(m_logFile);
}

void CLogFile::rollLogFile()
{
    m_writtenBytes = 0;
    //调用CLogfile的析构函数,将文件进行备份
    x_log::g_file_out.reset();
    //重新打开新的日志文件
    x_log::g_file_out = std::make_unique<CLogFile>(x_log::CLogger::LogFileName, false);
}

void CLogFile::setRollFlag(bool flag)
{
    m_isRoll = flag;
}

long CLogFile::getLogFileSize()
{
    long fileSize = -1;
    if (nullptr == m_logFile) return fileSize;
    fseek(m_logFile, 0L, SEEK_END);
    fileSize = ftell(m_logFile);
    return fileSize;
}
